<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2020/02/28
 * Time: 14:01
 */

namespace console\controllers\platform;

use business\base\models\BasicCategoryModel;
use yii\console\Controller;
use Exception;

class BasicCategoryController extends Controller
{
    /**
     * 同步三级分类名称到同义词配置表中的主词中
     * php yii platform/basic-category/rsync-to-synonym-words
     */
    public function actionRsyncToSynonymWords()
    {
        try {
            echo '开始时间' . microtime() . PHP_EOL;
            $categorys = $this->getBasicCategoryList(['is_leaf' => 1]);
            $i = 1;
            if (!empty($categorys)) {
                foreach ($categorys as $category) {
                    echo $i . ' ';
                    $categoryName = $category['category_name'];
                    if (!$this->getSynonymWords($categoryName)) {
                        $this->insertSynonymWords($categoryName);
                    }
                    $i++;
                }
            }
            echo '结束时间' . microtime()  . PHP_EOL;
        } catch (\Exception $ex){
            \Yii::error($ex->getMessage());
            echo '数据同步出错，错误信息：' . $ex->getMessage();
        }
    }

    /**
     * 新增同义词配置中的主词
     * @param $word
     * @return int
     * @throws \yii\db\Exception
     */
    protected function insertSynonymWords($word)
    {
        if (!empty($word)) {
            $sql =<<<SQL
            INSERT INTO `b2b_search`.`synonym_words_filter` (`word`, `index_key`, `created_by`, `updated_by`) VALUES (:word, :index_key, :created_by, :updated_by);
SQL;
            return \Yii::$app->getDb()->createCommand($sql,[
                ':word' => $word,
                ':index_key' => 'supplier',
                ':created_by' => 'sys',
                ':updated_by' => 'sys'
            ])->execute();
        }
    }

    /**
     * 返回同义词配置中的主词
     * @param $word
     * @return false|null|string
     * @throws \yii\db\Exception
     */
    protected function getSynonymWords($word)
    {
        if (!empty($word)) {
            $sql = <<<sql
            SELECT
                f.word
            FROM
                b2b_search.synonym_words_filter f
sql;
            $condition = " WHERE f.word = '$word' AND f.is_deleted = 0";
            $result = \Yii::$app->db->createCommand($sql . $condition)->queryScalar();

            return $result ? $result : '';
        }

        return '';
    }

    /**
     * 返回基础分类列表
     * @param array $cond
     * @return array|BasicCategoryModel
     * @throws \yii\db\Exception
     */
    protected function getBasicCategoryList($cond = [])
    {
        try {
            if (empty($result)) {
                $result = BasicCategoryModel::find()
                    ->where($cond)
                    ->orderBy("parent_code, display_order")
                    ->createCommand()
                    ->queryAll();
            }
            return $result;
        } catch (Exception $ex) {
            return new BasicCategoryModel();
        }
    }
}
